package com.wlts.wzy.KSUserModule.managerController;


import com.baomidou.mybatisplus.extension.service.IService;
import com.wlts.wzy.Base.*;
import com.wlts.wzy.KSUserModule.entity.KsStoreUser;
import com.wlts.wzy.KSUserModule.service.IKsStoreUserService;
import com.wlts.wzy.Result.Result;
import com.wlts.wzy.api.request.ks.AddKsStoreUserRequest;
import com.wlts.wzy.expand.MyLambdaQueryWrapper;
import com.wlts.wzy.expand.MyPage;
import com.wlts.wzy.util.CopyUtils;
import com.wlts.wzy.util.MD5;
import com.wlts.wzy.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 快手授权信息 前端控制器
 * </p>
 *
 * @author zhiyong
 * @since 2021-02-01
 */
@Api(tags = "快手用户 后台管理控制器")
@RestController
@RequestMapping("/manager/ks-store-user")
public class KsStoreUserManagerController {
    @Autowired
    private IKsStoreUserService ksStoreUserService;


    @ApiOperation(value = "增加")
    @RequiresRoles("admin")
    @RequestMapping(value = "/addEntity", method = RequestMethod.POST)
    public Result<KsStoreUser> addEntity(@RequestBody AddKsStoreUserRequest request) {

        MyLambdaQueryWrapper<KsStoreUser> userWrapper = new MyLambdaQueryWrapper<>();
        userWrapper.eq(BaseEntity::getDelFlag,"0");
        userWrapper.eq(KsStoreUser::getName,request.getName());
        KsStoreUser user=ksStoreUserService.getOne(userWrapper);

        if(user!=null){
            return Result.buildResult(500,"用户名已存在！");
        }

        KsStoreUser ksStoreUser=new KsStoreUser();
        CopyUtils.copyProperties(request,ksStoreUser);
        ksStoreUser.setUuid(System.currentTimeMillis() + StringUtils.getRandomIntByLength(2));
        ksStoreUser.setPassword(MD5.md5(ksStoreUser.getPassword()));
        ksStoreUserService.save(ksStoreUser);
        return Result.buildResult(ksStoreUser);
    }

    @ApiOperation(value = "删除")
    @RequiresRoles("admin")
    @RequestMapping(value = "/delById", method = RequestMethod.POST)
    public Result<KsStoreUser> delById(@RequestBody BaseIdParam t) {
        KsStoreUser t1 = ksStoreUserService.getById(t.getId());

        if (t1 != null) {
            t1.setDelFlag("1");
            ksStoreUserService.updateById(t1);
            return new Result<>();
        }
        return new Result<>(500,"id = " + t.getId() + " 不存在");
    }

    @ApiOperation(value = "修改")
    @RequestMapping(value = "/chaEntity", method = RequestMethod.POST)
    public Result<KsStoreUser> chaEntity(@RequestBody KsStoreUser ksStoreUser) {
        if (ksStoreUser.getNewPassword() != null) {
            ksStoreUser.setPassword(MD5.md5(ksStoreUser.getNewPassword()));
        }
        ksStoreUserService.updateById(ksStoreUser);
        return Result.buildResult(ksStoreUser);
    }

    @ApiOperation(value = "id查询")
    @RequiresRoles("admin")
    @RequestMapping(value = "/getById", method = RequestMethod.POST)
    public Result<KsStoreUser> getById(@RequestBody BaseIdParam t) {
        return Result.buildResult(ksStoreUserService.getById(t.getId()));
    }

    @ApiOperation(value = "分页查询")
    @RequestMapping(value = "/getPage", method = RequestMethod.POST)
    @RequiresRoles("admin")
    public Result<MyPage<KsStoreUser>> getPage(@RequestBody BasePageParam basePageParam) {

        MyLambdaQueryWrapper<KsStoreUser> userWrapper = new MyLambdaQueryWrapper<>();

        userWrapper.eq(BaseEntity::getDelFlag, "0");

        MyPage<KsStoreUser> ksStoreUserMyPage = ksStoreUserService.page(userWrapper, basePageParam.getCurrentPage(), basePageParam.getPageSize(), true);

        return Result.buildResult(ksStoreUserMyPage);
    }

}
